package hrm.action.insa.languageaction;

import hrm.action.Action;
import hrm.action.ActionForward;
import hrm.dto.insa.EmployeeDTO;
import hrm.dto.insa.LanguageDTO;
import hrm.dto.login.LoginDTO;
import hrm.serviceimple.EmployeeServiceImple;
import hrm.serviceimple.LanguageServiceImple;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InsaListLanguageAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward actfoward = new ActionForward();
		EmployeeServiceImple employeeService = EmployeeServiceImple.getInstance();
		LanguageServiceImple languageService = LanguageServiceImple.getInstance();
		
		EmployeeDTO dto = new EmployeeDTO();
		dto.setId(getFindId(request));
		dto = employeeService.getEmployee(dto);
		
		LanguageDTO languageDTO = new LanguageDTO();
		languageDTO.setId(getFindId(request));
		
		List<LanguageDTO> langList = languageService.getAllLanguage(languageDTO);	

		request.setAttribute("employeeDTO", dto);
		request.setAttribute("langList", langList);
		actfoward.setUrl("listLanguage");
		return actfoward;
	}
	
	private String getFindId(HttpServletRequest request){		
		String findid = (String) request.getSession().getAttribute("findid");
		if(findid == null){
			findid = ((LoginDTO)request.getSession().getAttribute("login")).getId();
			request.setAttribute("findid", findid);
		}
		return findid; 
	}

}
